package com.yungu.swift.admin.route.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.assets.driver.model.dto.CarModelDto;
import com.yungu.swift.assets.driver.model.param.*;
import com.yungu.swift.assets.driver.service.CarModelService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminBusinessPageParam;
import com.yungu.swift.base.model.admin.AdminUuidParam;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author : cuixiuyin
 * @date : 2019/6/17
 */
@RestController
@RequestMapping("/admin/v1/system/carModel")
@Api(value = "【系统服务-业务设置中心】", tags = "车型管理")
public class CarModelAdmin {

    @Reference
    private CarModelService carModelService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "车型分页查询", httpMethod = "POST")
    public ResponseData<PageVo<CarModelDto>> queryPage(@RequestBody AdminBusinessPageParam businessPageParam) {
        return carModelService.queryPage(businessPageParam);
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ApiOperation(value = "车型删除", httpMethod = "POST")
    public ResponseData<Boolean> del(@RequestBody AdminUuidParam uuidParam) {
        return carModelService.del(uuidParam.getUuid(), CarModelDto.class);
    }

    @RequestMapping(value = "/setDefault", method = RequestMethod.POST)
    @ApiOperation(value = "设置默认车型", httpMethod = "POST")
    public ResponseData<Boolean> setDefault(@RequestBody AdminCarModelParam carModelParam) {
        return carModelService.setDefault(Integer.parseInt(carModelParam.getBusinessType()), carModelParam.getUuid());
    }

    @RequestMapping(value = "/sort", method = RequestMethod.POST)
    @ApiOperation(value = "设置排序规则", httpMethod = "POST")
    public ResponseData<Boolean> sort(@RequestBody AdminCarModelSortParam carModelSortParam) {
        List<AdminCarModelSortParam.CarModelSortParam> list = carModelSortParam.getCarModelSortParams();
        if (CollectionUtils.isEmpty(list)) {
            return ResponseData.buildSuccessResponse("更新排序成功", Boolean.TRUE);
        }
        return carModelService.updateSort(carModelSortParam);
    }

    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "新增或者更新计价规则", httpMethod = "POST")
    public ResponseData<Boolean> saveOrUpdate(@RequestBody AdminCarModelAddParam carModelAddParam) {
        CarModelDto carModelDto = new CarModelDto();
        BeanUtils.copyProperties(carModelAddParam, carModelDto);
        return carModelService.saveOrUpdate(carModelDto, carModelAddParam.getUserUuid());
    }


    @RequestMapping(value = "/listByPointSite", method = RequestMethod.POST)
    @ApiOperation(value = "根据起终点获取车型数据", httpMethod = "POST")
    public ResponseData<List<CarModelDto>> listCarLevel(@RequestBody CarModelSiteParam siteParam) {
        if (ParamUtil.isIllegal(siteParam.getOriginUuid(), siteParam.getDestUuid())) {
            AdminErrorEnum paramError = AdminErrorEnum.PARAM_ERROR;
            return ResponseData.buildErrorResponse(paramError.getCode(), paramError.getMsg());
        }
        return carModelService.listCarModelByPointSite(siteParam);
    }

    @RequestMapping(value = "/listByWay", method = RequestMethod.POST)
    @ApiOperation(value = "根据路线获取车型数据", httpMethod = "POST")
    public ResponseData<List<CarModelDto>> listCarLevel(@RequestBody CarModelWayParam wayParam) {
        if (ParamUtil.isIllegal(wayParam.getWayUuid())) {
            AdminErrorEnum paramError = AdminErrorEnum.PARAM_ERROR;
            return ResponseData.buildErrorResponse(paramError.getCode(), paramError.getMsg());
        }
        return carModelService.listCarModelByWay(wayParam);
    }


}
